package com.damai.param;

import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

import com.citywithincity.utils.ClassUtil;
import com.citywithincity.utils.ValidateUtil;
import com.damai.error.ApiError;

public class Rules {
	/**
	 * 手机(String)
	 */
	public static final String phone = "phone";
	
	/**
	 * email(String)
	 */
	public static final String email = "email";
	
	/**
	 * 身份证(String)
	 */
	public static final String idCard = "idCard";
	
	/**
	 * 身份证(String)
	 */
	public static final String idCard18 = "idCard18";
	
	public static final String chinese = "chinese";
	
	/**
	 * 数字(String)
	 */
	public static final String integer = "integer";

	public static final String SPLITTER = "|";

	
	
	//public static final String maxLen10 = "maxLen[10]";
	
	
	public static IParamValidate getValidate(String[] rules,String[] messages){
		if(rules.length==1){
			if(rules[0].length()==0){
				return null;
			}
			return create(rules[0], messages[0]);
		}
		IParamValidate[] validates = new IParamValidate[rules.length];
		for(int i=0; i < rules.length; ++i){
			
			validates[i] = create(rules[i], messages[i]);
		}
		
		return new GroupParamValidate(validates);
	}
	
	private static Map<String, Class<? extends IParamValidate>> map = new ConcurrentHashMap<String, Class<? extends IParamValidate>>();
	static{
		map.put(phone, PhoneValidate.class);
		map.put("max", MaxValidate.class);
		map.put(idCard18, IDCard18Validatge.class);
		map.put(chinese, ChineseValidatge.class);
	}
	
	
	public static void register(String rule,Class<? extends IParamValidate> validateClass){
		map.put(rule, validateClass);
	}

	
	public static class ChineseValidatge extends AbsParamValidate{

		public ChineseValidatge(String message) {
			super(message);
		}

		@Override
		protected boolean doValidate(Object value) {
			return ValidateUtil.isChinese((String) value);
		}
		
	}
	
	
	public static class IDCard18Validatge extends AbsParamValidate{

		public IDCard18Validatge(String message) {
			super(message);
		}

		@Override
		protected boolean doValidate(Object value) {
			String error= ValidateUtil.isIDCardValidate(( String)value);
			if(error!=null){
				throw new ApiError(error);
			}
			return true;
		}
		
	}
	
	public static class PhoneValidate extends AbsParamValidate{

		public PhoneValidate(String message) {
			super(message);
		}

		@Override
		protected boolean doValidate(Object value) {
			
			return ValidateUtil.isMobile(( String)value);
		}
		
	}
	
	
	public static class MaxValidate extends AbsParamValidate{
		
		private int maxValue;
		
		public MaxValidate(String message,String[] args) {
			super(message);
			this.maxValue = Integer.parseInt(args[0]);
		}

		@Override
		protected boolean doValidate(Object value) {
			Integer integer = (Integer)value;
			
			return integer <= maxValue;
		}
	}
	
	public static IParamValidate create(String rule,String message,String[] args){
		Class<? extends IParamValidate> clazz = map.get(rule);
		if(clazz==null){
			throw new RuntimeException("Rule " + rule + " is not registered!");
		}
		try {
			return ClassUtil.newInstance(clazz, new Object[]{message,args});
		} catch (Exception e) {
			throw new RuntimeException("Rule " + rule + " init error!",e);
		}
	}
	
	public static IParamValidate create(String rule,String message){
		if(rule.contains(":")){
			String[] arr = rule.split(":");
			return create(arr[0], message ,arr[1].split(","));
		}
		
		
		Class<? extends IParamValidate> clazz = map.get(rule);
		if(clazz==null){
			throw new RuntimeException("Rule " + rule + " is not registered!");
		}
		try {
			return ClassUtil.newInstance(clazz, new Object[]{message});
		} catch (Exception e) {
			throw new RuntimeException("Rule " + rule + " init error!",e);
		}
	}
	
}
